﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Knihotoc_WP8.Data;
using Knihotoc_WP8.Localization;
using System.Windows.Media.Imaging;
using System.Windows.Resources;

namespace Knihotoc_WP8.Pages
{
    public partial class BookDetailPage : PhoneApplicationPage
    {
        private Book book;
        //private User owner;
        ///private Place currentLocation;
        private string[] bookState = new string[] { "TestingState", "New", "Free", "Borrowed", "NeedMaintenance" };

        public BookDetailPage()
        {
            this.InitializeComponent();

            book = new Book();

            (ApplicationBar.Buttons[0] as ApplicationBarIconButton).Text = Localization.LocalizedStrings.Get("ShowOnMapButton");
            (ApplicationBar.Buttons[1] as ApplicationBarIconButton).Text = Localization.LocalizedStrings.Get("FindBookButton");
            (ApplicationBar.Buttons[2] as ApplicationBarIconButton).Text = Localization.LocalizedStrings.Get("ReturnBookButton");
        }

        private string convertNumberToBookState(int n)
        {
            try
            {
                return bookState[n];
            }
            catch
            {
                return LocalizedStrings.Get("BookStateNotFound");
            }
        }

        private bool isCorrectBookPosition(float? lat, float? lon)
        {
            return (lat != null && lon != null);
        }

        private void ImageOnline_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            try
            {
                BitmapImage imageLocal = new BitmapImage();
                string path = this.book.CoverImagePath.Substring(1, this.book.CoverImagePath.Length - 1);
                StreamResourceInfo sr = Application.GetResourceStream(new Uri(path, UriKind.Relative));
                imageLocal.SetSource(sr.Stream);
                this.coverImage.Source = imageLocal;
            }
            catch (Exception)
            {
                StreamResourceInfo sr = Application.GetResourceStream(new Uri("Images/NoImage.jpg", UriKind.Relative));
                BitmapImage imageNoImage = new BitmapImage();
                imageNoImage.SetSource(sr.Stream);
                this.coverImage.Source = imageNoImage;
                return;
            }
        }

        #region User Input
        private void CoverImage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/ImagePage.xaml?ImagePath=" + this.book.CoverImagePath, UriKind.Relative));
        }

        private void ShowOnMapButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(String.Format("/Pages/MapPage.xaml?Latitude={0}&Longitude={1}", book.Lat, book.Lon), UriKind.Relative));
        }

        private void FindBookButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/BookFindPage.xaml?BookID=" + book.ID.ToString(), UriKind.Relative));
        }

        private void ReturnBookButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/BookReturnPage.xaml?BookID=" + book.ID.ToString(), UriKind.Relative));
        }
        #endregion
    }
}